//
//  BIZSynchronousTutorialListVC.m
//  biz
//
//  Created by LXL on 2017/7/5.
//  Copyright © 2017年 jieshuibao. All rights reserved.
//

#import "BIZSynchronousTutorialListVC.h"
#import "BIZSynchronousTutorialMoreCell.h"
#import "BIZSynchronousTutorialModel.h"
#import "BIZSynchronousTutorialVideoDetailVC.h"
#import "BIZSynchronousTutorialWordDetailVC.h"
#import "BIZMoviePlaySitcomListDataModel.h"
#import "BIZHotTopicDetailVC.h"

@interface BIZSynchronousTutorialListVC ()
@property (nonatomic, strong) NSMutableArray *synchronousTutorialModelArr;
@property (nonatomic, assign) NSInteger lastId;
@end

@implementation BIZSynchronousTutorialListVC
- (BOOL)shouldAutorotate {
    return NO;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return UIInterfaceOrientationPortrait;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"最新财税政策同步辅导";
    [self configTableView];
    [self loadSynchronousTutorialListData];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
    return self.synchronousTutorialModelArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BIZSynchronousTutorialMoreCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BIZSynchronousTutorialMoreCell" forIndexPath:indexPath];
    BIZSynchronousTutorialModel *model = self.synchronousTutorialModelArr[indexPath.section];
    cell.synchronousTutorialModel = model;
    
    MPWeakSelf(self);
//    cell.sychronousAnalysisWordDetailBlock = ^(){
//        DeLog(@"详情");
//        BIZHotTopicDetailVC *vc = [[BIZHotTopicDetailVC alloc] initWithUUID:model.uuid];
//        vc.hotTopicDetailType = BIZHotTopicDetailTypePolicy;
//        vc.titleStr = model.title;
//        vc.dateStr = [NSDate stringFromTimestamp:model.createDate];
//        [self.navigationController pushViewController:vc animated:YES];
//        
//    };
    
    cell.synchronousAnalysisBlock = ^(){
        DeLog(@"解析");
        BIZSynchronousTutorialVideoDetailVC *playHomeVC = [[BIZSynchronousTutorialVideoDetailVC alloc]initWithUuid:model.videoId];
        playHomeVC.synchronousTutorialModel = model;
        [weakself.navigationController pushViewController:playHomeVC animated:YES];

    };
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    BIZSynchronousTutorialWordDetailVC *vc = [[BIZSynchronousTutorialWordDetailVC alloc]init];
    vc.synchronousTutorialModel = self.synchronousTutorialModelArr[indexPath.section];
    vc.detailType = BIZSynchronousTutorialWordDetailTypeSynchronous;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)configTableView {
    self.tableView.estimatedRowHeight = 300;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.contentInset = UIEdgeInsetsMake(11-35, 0, 0, 0);
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.lastId = 1;
        [self.synchronousTutorialModelArr removeAllObjects];
        [self loadSynchronousTutorialListData];
    }];
    
    
    self.tableView.mj_footer = [MJRefreshBackNormalFooter  footerWithRefreshingBlock:^{
        [self loadSynchronousTutorialListData];
    }];
}

- (void)loadSynchronousTutorialListData {
    NSMutableDictionary *paramDic = [NSMutableDictionary dictionary];
    paramDic[@"pageIndex"] = @(self.lastId);
    paramDic[@"pageEnd"] = @(self.lastId+9);
    [MBProgressHUD showAnimationHUD];
    [RequestTool POST:kSynchronousTutorialListUrl parameters:paramDic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        DeLog(@"首页同步辅导列表》》》》》》》》》》：%@", responseObject);
        [MBProgressHUD hideHUD];
        [self stopRefresh];
        BIZSynchronousTutorialListModel *model = [BIZSynchronousTutorialListModel yy_modelWithJSON:responseObject];
        if (model.code == 1) {

            if (model.data.count) {
                self.lastId += model.data.count;
                [self.synchronousTutorialModelArr addObjectsFromArray:model.data];
                [self.tableView reloadData];
                
            } else {
                self.tableView.mj_footer.state = MJRefreshStateNoMoreData;
            }
           
            
        } else {
            [MBProgressHUD showErrorMessage:model.des];
            if (model.code == 2) {
                BIZLoginViewController *vc = [BIZLoginViewController viewControllerFromStoryboard];
                MPWeakSelf(self);
                vc.loginSuccessBlock = ^(){
                    [weakself.synchronousTutorialModelArr removeAllObjects];
                    [weakself loadSynchronousTutorialListData];
                };
                [self presentViewController:vc animated:YES completion:nil];
            }
        }
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        DeLog(@"首页同步辅导列表错误》》》》》》》》》》：%@", error);
        [self stopRefresh];
        if ([AFNetworkReachabilityManager sharedManager].networkReachabilityStatus == AFNetworkReachabilityStatusNotReachable) {
            [MBProgressHUD showErrorMessage:@"网络不给力"];
        }
        else {
            [MBProgressHUD showErrorMessage:@"服务器繁忙"];
        }
    }];
    
}


- (void)stopRefresh {
    !self.tableView.mj_header.isRefreshing ? : self.tableView.mj_header.endRefreshing;
    !self.tableView.mj_footer.isRefreshing ? : self.tableView.mj_footer.endRefreshing;
}

- (NSMutableArray *)synchronousTutorialModelArr{
    if (!_synchronousTutorialModelArr) {
        _synchronousTutorialModelArr = [NSMutableArray array];
    }
    
    return _synchronousTutorialModelArr;
}

- (NSInteger)lastId {
    if (!_lastId) {
        _lastId = 1;
    }
    
    return _lastId;
}



@end
